/**
 * helper class to display marketplace
 */


import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;



/**
 * @author Master
 * @version $Revision: 1.0 $
 */
public class MarketPlaceScreen extends JPanel{
	/**
	 * Field ERSHI.
	 * (value is 20)
	 */
	private static final int ERSHI = 20;

	/**
	 * Field SHI.
	 * (value is 10)
	 */
	private static final int SHI = 10;

	/**
	 * Field WU.
	 * (value is 5)
	 */
	private static final int WU = 5;

	/**
	 * Field SAN.
	 * (value is 3)
	 */
	private static final int SAN = 3;

	/**
	 * Field SI.
	 * (value is 4)
	 */
	private static final int SI = 4;

	/**
	 * Field SHIER.
	 * (value is 12)
	 */
	private static final int SHIER = 12;

	/**
	 * Method getBp.
	
	 * @return JPanel */
	public static JPanel getBp() {
		return Bp;
	}

	/**
	 * Method setBp.
	 * @param bp JPanel
	 */
	public static void setBp(JPanel bp) {
		MarketPlaceScreen.Bp = bp;
	}

	/**
	 * Method getScreenPanel.
	
	 * @return MarketPlacePanel */
	public static MarketPlacePanel getScreenPanel() {
		return ScreenPanel;
	}

	/**
	 * Method setScreenPanel.
	 * @param screenPanel MarketPlacePanel
	 */
	public static void setScreenPanel(MarketPlacePanel screenPanel) {
		MarketPlaceScreen.ScreenPanel = screenPanel;
	}

	/**
	 * Method getGameInfo.
	
	 * @return GameInfo */
	public static GameInfo getGameInfo() {
		return GameInfoIns;
	}

	/**
	 * Method setGameInfo.
	 * @param gameInfo GameInfo
	 */
	public static void setGameInfo(GameInfo gameInfo) {
		MarketPlaceScreen.GameInfoIns = gameInfo;
	}

	/**
	 * Method getMoney.
	
	 * @return JLabel */
	public static JLabel getMoney() {
		return Money;
	}

	/**
	 * Method setMoney.
	 * @param money JLabel
	 */
	public static void setMoney(JLabel money) {
		MarketPlaceScreen.Money = money;
	}

	/**
	 * Method getSpace.
	
	 * @return JLabel */
	public static JLabel getSpace() {
		return Space;
	}

	/**
	 * Method setSpace.
	 * @param space JLabel
	 */
	public static void setSpace(JLabel space) {
		MarketPlaceScreen.Space = space;
	}

	/**
	 * Method getStock.
	
	 * @return JLabel[] */
	public static JLabel[] getStock() {
		return Stock;
	}

	/**
	 * Method setStock.
	 * @param stock JLabel[]
	 */
	public static void setStock(JLabel[] stock) {
		MarketPlaceScreen.Stock = stock;
	}

	/**
	 * Method getCurrentFuel.
	
	 * @return JLabel */
	public static JLabel getCurrentFuel() {
		return CurrentFuel;
	}

	/**
	 * Method setCurrentFuel.
	 * @param currentFuel JLabel
	 */
	public static void setCurrentFuel(JLabel currentFuel) {
		MarketPlaceScreen.CurrentFuel = currentFuel;
	}

	/**
	 * Method getTechLevel.
	
	 * @return JLabel */
	public static JLabel getTechLevel() {
		return TechLevel;
	}

	/**
	 * Method setTechLevel.
	 * @param techLevel JLabel
	 */
	public static void setTechLevel(JLabel techLevel) {
		MarketPlaceScreen.TechLevel = techLevel;
	}

	/**
	 * Field bp.
	 */
	public static JPanel Bp=null;

	/**
	 * Field screenPanel.
	 */
	public static MarketPlacePanel ScreenPanel=null;

	/**
	 * Field gameInfo.
	 */
	public static GameInfo GameInfoIns=null;

	/**
	 * Field techLevel.
	 */
	/**
	 * Field currentFuel.
	 */
	/**
	 * Field stock.
	 */
	/**
	 * Field space.
	 */
	/**
	 * Field money.
	 */
	public static JLabel Money=null, Space=null,
			Stock[]=null, CurrentFuel=null, TechLevel=null;

	/**
	 * Method start.
	 * @param p Planet
	 */
	public static void start(Planet p){
        ConfigurationScreen.MyFrame.getContentPane().removeAll();
        ScreenPanel = new MarketPlacePanel();
        GoodInfo.init(p);
        ScreenPanel.setLayout(new GridLayout(SHI, SHIER));
		final String pName=p.getName();
		final int indexSpace=pName.indexOf(' ');
		ScreenPanel.add(new JLabel(pName.substring(0, indexSpace)));
		ScreenPanel.add(new JLabel(pName.substring(indexSpace)));
		ScreenPanel.add(new JLabel("Event:"));
		ScreenPanel.add(new JLabel(p.getSpecialCondition()));
		ScreenPanel.add(new JLabel("Tech Level:"));
		TechLevel=new JLabel("" + PlayerInfo.getCurrentSolarSystem().getTechLevel());
		ScreenPanel.add(TechLevel);
		ScreenPanel.add(new JLabel("TaxRate:"));
		ScreenPanel.add(new JLabel("" + Planet.getGovernment().getRate()));
		for (int i=0;i < SI;i++) ScreenPanel.add(new JLabel(""));
		ScreenPanel.add(new JLabel("Space"));
		Space=new JLabel("" + PlayerInfo.getCargoAvailable());
		ScreenPanel.add(Space);
		ScreenPanel.add(new JLabel("money"));
		Money=new JLabel("" + PlayerInfo.getMoney());
		ScreenPanel.add(Money);
		final JLabel fuel = new JLabel("currentFuel: ");
		CurrentFuel = new JLabel(Integer.toString(PlayerInfo.getFuel()));
		ScreenPanel.add(fuel);
		ScreenPanel.add(CurrentFuel);
		final JButton addfuel = new JButton("add fuel");
		addfuel.addActionListener(new FuelListener(CurrentFuel, Money));
		ScreenPanel.add(addfuel);
		
		for (int i=0;i < SAN;i++) ScreenPanel.add(new JLabel(""));
		ScreenPanel.add(new JLabel("GOOD: "));
		for (String name:GoodInfo.getName()){
			JLabel good = new JLabel(name);
			ScreenPanel.add(good);
		}
		ScreenPanel.add(new JLabel("Tech Level: "));
		for (int tl:GoodInfo.getMP()){
			JLabel mp=new JLabel("" + tl + "");
			ScreenPanel.add(mp);
		}
		ScreenPanel.add(new JLabel("MTLU: "));
		for (int mli:GoodInfo.getMU()){
			JLabel ml=new JLabel("" + mli + "");
			ScreenPanel.add(ml);
		}
		ScreenPanel.add(new JLabel("BASEPRICE: "));
		for (int bpi:GoodInfo.getMODELEDPRICE()){
			JLabel bp=new JLabel("" + bpi + "");
			ScreenPanel.add(bp);
		}
		ScreenPanel.add(new JLabel("You Own: "));
		Stock=new JLabel[GoodInfo.GoodName.length];
		for (int i=0;i < GoodInfo.GoodName.length;i++){
			Stock[i]=new JLabel("" + PlayerInfo.getStock(i) + "");
			ScreenPanel.add(Stock[i]);
		}
		ScreenPanel.add(new JLabel(""));
		for (String name:GoodInfo.getName()){
			JButton buy=new JButton("Buy");
			buy.addActionListener(new BuyListener(p, name, GameInfoIns));
			ScreenPanel.add(buy);
		}
		ScreenPanel.add(new JLabel(""));
		for (String name:GoodInfo.getName()){
			JButton sell=new JButton("Sell");
			sell.addActionListener(new SellListener(p, name, GameInfoIns));
			ScreenPanel.add(sell);
		}
		
		final JButton saveButton=new JButton("Save");
		saveButton.addActionListener(new SaveListener());
		ScreenPanel.add(saveButton);
		final JButton upgradeButton=new JButton("Upgrade");
		upgradeButton.addActionListener(new UpgradeListener());
		ScreenPanel.add(upgradeButton);
		final JButton info=new JButton("Player");
		info.addActionListener(new PlayerInfoListener());
		ScreenPanel.add(info);

		for (int i=0;i < SAN;i++) ScreenPanel.add(new JLabel(""));

		final JButton spaceship=new JButton("Ship");
		spaceship.addActionListener(new SpaceshipListener());
		ScreenPanel.add(spaceship);
		final JButton item=new JButton("Item");
		item.addActionListener(new ItemListener());
		ScreenPanel.add(item);
		final JButton mission=new JButton("Mission");
		mission.addActionListener(new MissionListener());
		ScreenPanel.add(mission);
		final JButton insurance=new JButton("Insurance");
		insurance.addActionListener(new InsuranceListener());
		ScreenPanel.add(insurance);
		final JButton returnButton=new JButton("Return");
		returnButton.addActionListener(new BackToUniverseListener());
		ScreenPanel.add(returnButton);

		ConfigurationScreen.MyFrame.getContentPane().setBackground(Color.yellow);
		ConfigurationScreen.MyFrame.setContentPane(ScreenPanel);
		ConfigurationScreen.MyFrame.pack();
		ConfigurationScreen.MyFrame.setVisible(true);
		boolean gameOver=true;
		
		for (int i=0;i < PlayerInfo.GoodOwned.length;i++){
			if (PlayerInfo.GoodOwned[i] != 0) gameOver=false;
		}
		if (GameInfo.getPilotSP() >= WU && PlayerInfo.getMoney() >
			SHI * (PlayerInfo.FuelCapacity - PlayerInfo.getFuel()) *
			(ERSHI - GameInfo.getPilotSP()) / ERSHI) gameOver=false;
		if (PlayerInfo.getMoney() > SHI * (PlayerInfo.FuelCapacity -
				PlayerInfo.getFuel())) gameOver=false;
		if (gameOver && PlayerInfo.getFuel() < ERSHI) GameOverScreen.start();
	}

	/**
	 * Method upDate.
	 * @param str String
	 */
	public void upDate(String str){
		JOptionPane.showMessageDialog( ConfigurationScreen.MyFrame, str);
	}
}